// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database();

async function querryAritcle(){
  try {
    return await db.collection('aritcle').where({
      status:1
    }).orderBy('_createTime','desc').get({
      success:function(res){
        return res
      }
    });
  } catch (error) {
    console.log(error);
  }
}
async function querryAritcleConetnt(data){
  try {
    return await db.collection('aritcle').where(data.whereData).get({
      success:function(res){
        return res
      }
    });
  } catch (error) {
    console.log(error);
  }
}

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  console.log(wxContext.OPENID);
  switch(event.functionName) {// 判断用户使用什么方法
    case "querryAritcle":
      let aritcle = await querryAritcle();
      return {result:aritcle};
    case "querryAritcleConetnt":
      let aritclecontent = await querryAritcleConetnt(event.data);
      return {result:aritclecontent};
  }
  console.log("fail");
  return {
      result: {
          errMsg: 'no',
      }
    }
}